package gui.win.data.add;

import core.utils.Utils;
import gui.Segl1;
import gui.processor.Processor;
import gui.processor.Worker;
import gui.utils.Para;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JOptionPane;
import javax.swing.JPanel;

/*Author: John Archer, Contact: john.archer.jpa@gmail.com*/
public class Buttons extends JPanel implements ActionListener{
    private JButton close, addData;
    private AddD addD;
    private Segl1 L1_RpgsGui;
    public Buttons(Segl1 L1_RpgsGui, AddD addD){
        super(new BorderLayout());
        this.setBorder(BorderFactory.createEmptyBorder(0,0,0,0));
        this.L1_RpgsGui = L1_RpgsGui;
        this.addD = addD;

        this.add(Utils.getButton(close = new JButton("CANCEL"), 184, 22, new Color(102, 102, 102), new Color(178, 178, 178), this, "CANCEL"), BorderLayout.WEST);
        this.add(Utils.getButton(addData = new JButton("ADD"), 184, 22, new Color(102, 102, 102), new Color(178, 178, 178), this, "ADD"), BorderLayout.EAST);
    }

    public void actionPerformed(ActionEvent evt) {
        Object source = evt.getSource();
        if(source == close){addD.dispose();}
        else if(source == addData){
            if(addD.getDatasetTitle().equals("") || addD.getDatasetTitle()==null){L1_RpgsGui.msg("Dataset title not entered");}
            else if(addD.getPathToDataset().equals("") || addD.getPathToDataset()==null){L1_RpgsGui.msg("Dataset path not entered");}
            else if(Para.P_ID_ADD_RM==-1){L1_RpgsGui.msg("Please select the project to add data to");}
            else{
                if(!new File(addD.getPathToDataset()).exists()){JOptionPane.showMessageDialog(null, "Dataset file does not exist.", "alert", JOptionPane.ERROR_MESSAGE);}
                else{
                    Para.D_TITLE = addD.getDatasetTitle();
                    Para.D_PATH = addD.getPathToDataset();
                    Processor processor = new Processor(L1_RpgsGui);
                    processor.setTask(Para.TASK_ADD_DATA);
                    Worker sl = new Worker(processor);
                    sl.execute();
                    closeFrame();
                }
            }
        }
    }
    public void closeFrame(){addD.closeFrame();}
}